/**
 * 
 */
package es.drOsu.game;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.CheckBox;

import com.openfeint.api.OpenFeint;

import es.drOsu.common.Constants;

// TODO: Auto-generated Javadoc
/**
 * The Class Settings.
 */
public class Settings extends Activity implements OnClickListener {

	
	/** The Constant TAG. */
	private static final String TAG = "Settings";
	
    /* (non-Javadoc)
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
	    requestWindowFeature(Window.FEATURE_NO_TITLE);
	    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
	    setContentView(R.layout.settings);
	    SharedPreferences prefs = getSharedPreferences(OpenFeint.getCurrentUser().userID(),Context.MODE_PRIVATE);
	    
	    CheckBox soundsCheckBox = (CheckBox) findViewById(R.id.sounds_checkbox);
	    if(!prefs.contains(Constants.sharedPreferencesSoundsEffects)){
	    	soundsCheckBox.setChecked(false);
	    	Log.d(TAG, "uncheck1");
	    	SharedPreferences.Editor editor = prefs.edit();
			editor.putString(Constants.sharedPreferencesSoundsEffects, Constants.sharedPreferencesSoundEffectsUncheck);
			editor.commit();
		   
	    }else{
	    	 if(prefs.getString(Constants.sharedPreferencesSoundsEffects, Constants.sharedPreferencesSoundEffectsUncheck).contains(Constants.sharedPreferencesSoundEffectsUncheck)){
	    		 Log.d(TAG, "uncheck");
			    	soundsCheckBox.setChecked(false);
			 }else{ 
				 Log.d(TAG, "check");
				 soundsCheckBox.setChecked(true);
			 }
	    }
        soundsCheckBox.setOnClickListener(this);
        
        CheckBox graphicsCheckBox = (CheckBox) findViewById(R.id.graphics_checkbox);
        if(!prefs.contains(Constants.sharedPreferencesGraphicsEffects)){
	    	graphicsCheckBox.setChecked(false);
	    	SharedPreferences.Editor editor = prefs.edit();
			editor.putString(Constants.sharedPreferencesGraphicsEffects, Constants.sharedPreferencesGraphicsEffectsUncheck);
			editor.commit();
		   
	    }else{
	    	 if(prefs.getString(Constants.sharedPreferencesGraphicsEffects, Constants.sharedPreferencesGraphicsEffectsUncheck).contains(Constants.sharedPreferencesGraphicsEffectsUncheck)){
			    	graphicsCheckBox.setChecked(false);
			 }else 
				 graphicsCheckBox.setChecked(true);
	    }
        graphicsCheckBox.setOnClickListener(this);
    }
	
	/* (non-Javadoc)
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v) {
		// Perform action on clicks, depending on whether it's now checked
		SharedPreferences prefs = getSharedPreferences(OpenFeint.getCurrentUser().userID(),Context.MODE_PRIVATE);
		switch (v.getId()){
    		case R.id.sounds_checkbox:
    			if(((CheckBox) v).isChecked()){
					SharedPreferences.Editor editor = prefs.edit();
					editor.putString(Constants.sharedPreferencesSoundsEffects, Constants.sharedPreferencesSoundEffectsCheck);
					editor.commit();
					
    			}else{
					SharedPreferences.Editor editor = prefs.edit();
					editor.putString(Constants.sharedPreferencesSoundsEffects, Constants.sharedPreferencesSoundEffectsUncheck);
					editor.commit();
    			}
    			break;
    		case R.id.graphics_checkbox:
    			if(((CheckBox) v).isChecked()){
					SharedPreferences.Editor editor = prefs.edit();
					editor.putString(Constants.sharedPreferencesGraphicsEffects, Constants.sharedPreferencesGraphicsEffectsCheck);
					editor.commit();
					
    			}else{
					SharedPreferences.Editor editor = prefs.edit();
					editor.putString(Constants.sharedPreferencesGraphicsEffects, Constants.sharedPreferencesGraphicsEffectsUncheck);
					editor.commit();
    			}
    			break;
    			
		}

		
	}
	/* (non-Javadoc)
	 * @see android.app.Activity#onKeyDown(int, android.view.KeyEvent)
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode == KeyEvent.KEYCODE_BACK){
			Intent intentMain = new Intent(this,DrosuMain.class);			 
			 this.startActivity(intentMain);
			return true;
		}else
			return super.onKeyDown(keyCode, event);
	}
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onPause()
	 */
	@Override
	protected void onPause(){
		super.onPause();
		Log.d(TAG, "onPause()");
		finish();
	}
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onRestart()
	 */
	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		Log.d(TAG, "onRestart");
		finish();
	}

}
